<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="3">
              <el-form-item label="门店" prop="storeId">
                <el-select
                  v-model="queryParams.storeId"
                  placeholder="请选择"
                  filterable
                >
                  <el-option
                    v-for="(item, index) in $store.state.user.stores"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item prop="orderNo" label="订单号">
                <el-input
                  class="w-200"
                  v-model="queryParams.orderNo"
                  placeholder="请输入订单号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <div class="pay_ipt">
                <el-form-item label="用户" prop="userId">
                  <el-select
                    v-model="queryParams.userId"
                    placeholder="请选择"
                    class="w-200"
                    filterable
                    remote
                    :remote-method="remoteMethod"
                    :loading="userloading"
                  >
                    <el-option
                      v-for="item in userList"
                      :key="item.id"
                      :label="item.nickname"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="4">
              <div class="pay_ipt">
                <el-form-item label="类型" prop="awardType">
                  <el-select
                    class="w-160"
                    v-model="queryParams.awardType"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in CommissionType"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="5">
              <!-- <el-form-item label="时间" prop="time">
                <el-date-picker
                  v-model="queryParams.time"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item> -->
              <el-form-item label="时间" prop="date">
                <timer
                  ref="timer"
                  :timer.sync="queryParams.date"
                  @query="handleQuery"
                  :isCheck="false"
                ></timer>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">提成明细</div>
        <div class="op_right">
          <!-- v-hasPermi="['order:finance:store:revenue:exl']" -->
          <el-button
            @click="handleExport"
            :loading="exportLoading"
            class="repeat"
            >导出</el-button
          >
        </div>
      </div>

      <div class="table_list">
        <el-table
          :data="VerificationList"
          stripe
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="storeName"
            label="门店名称"
            :show-overflow-tooltip="true"
            width="170px"
          >
          </el-table-column>
          <el-table-column prop="userName" label="姓名"> </el-table-column>
          <el-table-column prop="stageName" label="艺名"> </el-table-column>
          <el-table-column prop="awardMoney" label="提成金额">
          </el-table-column>
          <el-table-column prop="itemName" label="项目名称"> </el-table-column>
          <el-table-column prop="roomName" label="房间名称"> </el-table-column>
          <el-table-column prop="orderNo" label="订单编号"> </el-table-column>
          <el-table-column prop="orderOpen" label="时间"> </el-table-column>
          <el-table-column prop="awardTypeStr" label="提成类型">
            <!-- <template slot-scope="scope">
              <div>{{ awardTypeObj[scope.row.awardType] }}</div>
            </template> -->
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  getAwardPage,
  getAwardOrder,
  getAwardorderServer,
  ExcelCommission,
  getAwardType,
} from "@/api/bpm/commission";
import timer from "@/components/getTime/timer";
import { getTimer } from "@/utils/utils";
import { listUser } from "@/api/system/user";
export default {
  name: "Storeadmincommission",
  components: { timer },
  data() {
    return {
      VerificationList: [],
      loading: false,
      userloading: false,
      OrderList: [],
      value: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        awardType: "", // 提成类型
        orderNo: "", // 订单编号
        startTime: "", //开始时间 yyyy-MM-dd HH:mm:ss
        endTime: "", // 结束时间
        storeId: "",
        userId: "",
        date: [],
      },
      total: 0,
      exportLoading: false,

      awardTypeObj: {
        HA: "手工提成",
        CA: "加钟提成",
        CS: "点钟提成",
        WO: "加班提成",
        HO: "待客提成",
        VI: "约客提成",
        RC: "充值提成",
      },

      CommissionType: [
        // {
        //   value: "HA",
        //   label: "手工提成",
        // },
        // {
        //   value: "CA",
        //   label: "加钟提成",
        // },
        // {
        //   value: "CS",
        //   label: "点钟提成",
        // },
        // {
        //   value: "WO",
        //   label: "加班提成",
        // },
        // {
        //   value: "HO",
        //   label: "待客提成",
        // },
        // {
        //   value: "VI",
        //   label: "约客提成",
        // },
        // {
        //   value: "RC",
        //   label: "充值提成",
        // },
      ],

      OrderOptions: [],
      Loadings: false,
      userList: [], // 用户
      orderServer: [], // 订单服务
      ExcelAwardPage: false,
      isEditing: false,
      searchUser: null,
    };
  },

  mounted() {},

  created() {
    this.getList();
    this.getLists();
    this.getAwardTypeList();
  },

  methods: {
    // 获得提成类型下拉数据
    getAwardTypeList(){
      getAwardType().then(res=>{
        const {code,data} = res;
        if(code === 0){
          this.CommissionType = data;
        }
      }).catch(err=>{

      });
    },

    handleExport() {
      if (!this.queryParams.awardType) {
        this.$message.error("请先选择类型");
        return false;
      }

      if (!!this.queryParams.date && this.queryParams.date.length === 0) {
        this.$message.error("请先选择时间范围");
        return false;
      }

      let queryParams = JSON.parse(JSON.stringify(this.queryParams));
      if (!!this.queryParams.date) {
        queryParams.startTime = this.queryParams.date[0];
        queryParams.endTime = this.queryParams.date[1];
      }
      // delete queryParams.date;

      let startTime = new Date(queryParams.startTime).getTime(); //得到毫秒数
      let endTime = new Date(queryParams.endTime).getTime(); //得到毫秒数

      console.log(startTime, "startTime");
      console.log(endTime, "endTime");
      if (endTime - startTime > 31 * 24 * 60 * 60 * 1000) {
        console.log(endTime - startTime);
        this.$message.error("限制时间范围天数只能选择一个月内");
        return false;
      }

      this.$modal
        .confirm("是否确认导出提成明细数据?")
        .then(() => {
          this.exportLoading = true;

          return ExcelCommission(queryParams);
        })
        .then((response) => {
          this.$download.excel(response, "提成明细");
          this.exportLoading = false;
        })
        .catch(() => {
          this.exportLoading = false;
        });
    },

    /** 查询用户列表 */
    remoteMethod(query) {
      if (query !== "") {
        this.userList = [];
        this.userloading = true;
        clearTimeout(this.searchUser);
        this.searchUser = setTimeout(() => {
          this.getLists(query);
        }, 500);
      } else {
        this.userList = [];
      }
    },
    getLists(query) {
      this.userloading = true;
      listUser({ keyword: query }).then((response) => {
        this.userList = response.data.list;
        this.totals = response.data.total;
        this.userloading = false;
      });
    },
    SelectServer(orderNo) {
      this.getAwardorderServerInfo({
        orderId: orderNo,
      });
    },

    getOrder(query) {
      this.Loadings = true;
      getAwardOrder({
        orderNo: query,
      })
        .then((res) => {
          this.Loadings = false;
          const { code, data } = res;
          if (code === 0) {
            this.OrderList = data;
          }
        })
        .catch((err) => {
          this.Loadings = false;
        });
    },
    handleClose() {
      this.resetFormData("ruleForm");
      this.$nextTick(() => {
        this.dialogVisible = false;
      });
    },

    // 添加提成查询订单服务
    getAwardorderServerInfo(row) {
      getAwardorderServer({ orderId: row.orderId })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.orderServer = data;
          }
        })
        .catch((err) => {});
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 提成明细列表
    getList() {
      this.loading = true;
      let queryParams = JSON.parse(JSON.stringify(this.queryParams));

      if (!!this.queryParams.date) {
        queryParams.startTime = this.queryParams.date[0];
        queryParams.endTime = this.queryParams.date[1];
      }
      delete queryParams.date;
      getAwardPage(queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.VerificationList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.goodTop {
  width: 100%;
  height: 48px;
  background: #ffffff;
  display: flex;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.6);
  line-height: 48px;
  padding-left: 16px;
  .good_list {
    margin-right: 32px;
  }

  .good_active {
    color: #0052d9;
    border-bottom: 1px solid #0052d9;
  }
}

.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.Order_box {
  display: flex;
  margin-left: -50px;
  .select_ipt {
    width: 130px;
    margin-right: 10px;
  }
}

.pay_ipt {
  margin-left: 50px;
}

.order_ipt {
  margin-left: -120px;
}
</style>
